﻿{% extends "gma/base.html" %}
{% load mytag %}

<!-- CONTENT
  =====================================================================-->

{% block content %}

<div class="page__bd">
    <div class="weui-cells__title"> 基本数据 </div>
    <div class="weui-cells">
        {% if user.avatar %}
        <a class="weui-cell weui-cell_access" href="{% url 'gma:gallery' user.avatar %}">
            <div class="weui-cell__bd"> <p> 头像 </p> </div>
            <div class="weui-cell__ft"> <img src="{{ user.avatar|cdn }}" class="right_large_avatar" class="avatar"> </div>
        </a>
        {% endif %}

        <div class="weui-cell">
            <div class="weui-cell__bd"> <p> 用户id </p> </div>
            <div class="weui-cell__ft">{{ user.user_id }}</div>
        </div>

        <div class="weui-cell">
            <div class="weui-cell__bd"> <p> 昵称 </p> </div>
            <div class="weui-cell__ft">{{ user.nickname }}</div>
        </div>

        <div class="weui-cell">
            <div class="weui-cell__bd"> <p> 国家 </p> </div>
            <div class="weui-cell__ft">{{ user.country }}</div>
        </div>

        <div class="weui-cell">
            <div class="weui-cell__bd"> <p> 省份 </p> </div>
            <div class="weui-cell__ft">{{ user.province }}</div>
        </div>

        <div class="weui-cell">
            <div class="weui-cell__bd"> <p> 城市 </p> </div>
            <div class="weui-cell__ft">{{ user.city }}</div>
        </div>
    </div>

    <div class="weui-cells__title"> 权限等级 </div>
    <div class="weui-cells">
        <a class="weui-cell weui-cell_access" id="showGmLevelSettingActionSheet">
            <div class="weui-cell__bd"> <p> 权限等级 </p> </div>
            <div class="weui-cell__ft">{{ level_desp }}</div>
        </a>
    </div>

    <div class="weui-cells__title"> 权限开关 </div>
    <div class="weui-cells">
        {% for permission_key, permission in permissions.items %}
        <div class="weui-cell">
            <div class="weui-cell__bd"> <p> {{ permission.name }} </p> </div>
            <div class="weui-cell__ft">
                <label for="switch_{{ permission_key }}" class="weui-switch-cp">
                    <input id="switch_{{ permission_key }}" class="weui-switch-cp__input" type="checkbox" {{ permission.checked }} onclick="clickPermissionSwitch('#switch_{{ permission_key }}', {{ permission.pid }}, '{{ permission.name }}')" />
                    <div class="weui-switch-cp__box"></div>
                </label>
            </div>
        </div>
        {% endfor %}
    </div>
</div>

<!-- 设置用户权限等级的弹出菜单 -->
<div>
    <div class="weui-mask" id="gmLevelSettingMask" style="display: none"></div>
    <div class="weui-actionsheet" id="gmLevelSettingActionsheet">
        <div class="weui-actionsheet__title">
            <p class="weui-actionsheet__title-text"> 设置用户权限等级 </p>
        </div>
        <div class="weui-actionsheet__menu">
            {% if user.level != 1 %}
            <div class="weui-actionsheet__cell" onclick="window.location = &quot;{% url 'gma:set_gm_level' user.user_id 1 %}&quot;"> 1级: 无权限用户 </div>
            {% endif %}

            {% if user.level != 2 %}
            <div class="weui-actionsheet__cell" onclick="window.location = &quot;{% url 'gma:set_gm_level' user.user_id 2 %}&quot;"> 2级: 普通管理员 </div>
            {% endif %}

            {% if user.level != 3 %}
            <div class="weui-actionsheet__cell" onclick="window.location = &quot;{% url 'gma:set_gm_level' user.user_id 3 %}&quot;"> 3级: 高级管理员 </div>
            {% endif %}
        </div>
        <div class="weui-actionsheet__action">
            <div class="weui-actionsheet__cell" id="gmLevelActionsheetCancel"> 取消 </div>
        </div>
    </div>
</div>

{#<div class="weui-btn-area">#}
{#    <a class="weui-btn weui-btn_block weui-btn_warn" href="{% url 'gma:delete' user.user_id %}"> 删除管理员 </a>#}
{#</div>#}

<div class="page__ft">
    <a href="{% url 'gma:home' %}">
        <span class="bottom-logo">
           HiBao
       </span>
    </a>
</div>


{% endblock content %}

<!-- SCRIPT
  =====================================================================-->

{% block extra_js %}
<script>

$(function() {
    var $actionsheet = $('#gmLevelSettingActionsheet');
    var $mask = $('#gmLevelSettingMask');

    function hideActionSheet() {
        $actionsheet.removeClass('weui-actionsheet_toggle');
        $mask.fadeOut(200);
    }

    $mask.on('click', hideActionSheet);

    $('#gmLevelActionsheetCancel').on('click', hideActionSheet);

    $("#showGmLevelSettingActionSheet").on("click", function() {
        $actionsheet.addClass('weui-actionsheet_toggle');
        $mask.fadeIn(200);
    });
});

function clickPermissionSwitch(widget, pid, name) {
    if ($(widget).is(':checked')) {
        let url = "{% url 'gma:api_set_gm_permission' %}";
        let data = {
            'uid': {{ user.user_id }},
            'pid': pid,
        };

        new my_ajax(url, data, '', '', () => {
            show_toast(`开启${name}!`);

        });
    } else {
        let url = "{% url 'gma:api_unset_gm_permission' %}";
        let data = {
            'uid': {{ user.user_id }},
            'pid': pid,
        };

        new my_ajax(url, data, '', '', () => {
            show_toast(`关闭${name}!`);

        });
    }
}
</script>
{% endblock extra_js %}